package com.ytt.core.service.service.scores;


import com.ytt.core.model.common.PageDataDTO;
import com.ytt.core.model.scores.ScoresDto;
import com.ytt.core.model.scores.ScoresEditDto;
import com.ytt.core.model.student.StudentDto;
import com.ytt.core.model.user.UserDto;
import com.ytt.core.service.entity.scores.Scores;
import com.ytt.core.service.exception.NoSuchDataException;
import com.ytt.core.service.mapper.scores.ScoresMapper;
import com.ytt.core.service.repository.ScoresRepository;
import com.ytt.core.service.service.ProposalQueryParam;
import com.ytt.core.service.utils.BaseUtil;
import com.ytt.core.service.utils.PageDataUtil;
import com.ytt.core.util.jpa.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Service("scoresService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ScoresImpl implements ScoresService {
    private  final ScoresRepository scoresRepository;
    private  final ScoresMapper scoresMapper;

    private static final String PASS_PLACE_HOLDER = "$pWd$(C%#*(78))!<>";
    @Autowired
    public ScoresImpl(ScoresRepository scoresRepository, ScoresMapper scoresMapper) {
        this.scoresRepository = scoresRepository;
        this.scoresMapper = scoresMapper;
    }

    @Override
    public ScoresDto findById(String id) {
        Scores notification = this.scoresRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        ScoresDto scoresDto=this.scoresMapper.toDTO(notification);

        UserDto userDto=scoresDto.getStudentId().getUser();
        userDto.setPassword(PASS_PLACE_HOLDER);

        StudentDto studentDto=scoresDto.getStudentId();
        studentDto.setUser(userDto);

       scoresDto.setStudentId(studentDto);

        return scoresDto;
    }

    @Override
    public List<ScoresDto> findAll(String sort, ProposalQueryParam param) {
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<Scores> notifications = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(),sort1);
        return notifications.stream().map(this.scoresMapper::toDTO).collect(Collectors.toList()).stream().peek(n->{
            UserDto userDto=n.getStudentId().getUser();
            //设置用户的密码 防止密码返回真实的数据泄露
            userDto.setPassword(PASS_PLACE_HOLDER);
            StudentDto studentDto=n.getStudentId();
            studentDto.setUser(userDto);
            n.setStudentId(studentDto);
        }).collect(Collectors.toList());

    }

    @Override
    public PageDataDTO<ScoresDto> findOnePage(Integer page, Integer size, String sort, ProposalQueryParam param) {
        Page<Scores> notificationPage = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(), PageHelper.generatePageRequest(page, size, sort));
        List<ScoresDto> notificationDTOList = notificationPage.getContent().stream().map(this.scoresMapper::toDTO)
                .collect(Collectors.toList()).stream().peek(n->{
                    UserDto userDto=n.getStudentId().getUser();
                    //设置用户的密码 防止密码返回真实的数据泄露
                    userDto.setPassword(PASS_PLACE_HOLDER);
                    StudentDto studentDto=n.getStudentId();
                    studentDto.setUser(userDto);
                    n.setStudentId(studentDto);
                }).collect(Collectors.toList());
        return PageDataUtil.toPageData(notificationPage, notificationDTOList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ScoresDto create(ScoresEditDto saveDTO) {
        Scores entity = new Scores();
        this.scoresMapper.updateEntity(saveDTO, entity);
        entity = this.scoresRepository.save(entity);
        return this.scoresMapper.toDTO(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ScoresDto update(String id, ScoresEditDto saveDTO) {
        Optional<Scores> optional = this.scoresRepository.findById(Long.valueOf(id));
        Scores entity = optional.orElseThrow(() -> new NoSuchDataException(id));

        this.scoresMapper.updateEntity(saveDTO, entity);
        entity = this.scoresRepository.saveAndFlush(entity);
        return this.scoresMapper.toDTO(entity);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        Scores entity = this.scoresRepository.findById(Long.valueOf(id)).orElseThrow(() -> new NoSuchDataException(id));
        this.scoresRepository.delete(entity);
    }

    @Override
    public int passScoreBySchoolClass(String sort,ProposalQueryParam param) {
        final int[] le = {0};

        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<Scores> notifications = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(),sort1);
        List<ScoresDto> list= notifications.stream().map(this.scoresMapper::toDTO).collect(Collectors.toList()).stream().peek(n->{
            UserDto userDto=n.getStudentId().getUser();
            //设置用户的密码 防止密码返回真实的数据泄露
            userDto.setPassword(PASS_PLACE_HOLDER);
            StudentDto studentDto=n.getStudentId();
            studentDto.setUser(userDto);
            n.setStudentId(studentDto);
            if(param.getSchoolClass()!=null&&!param.getSchoolClass().equals("")&&!"null".equals(param.getSchoolClass())){
                if(userDto.getSchoolClass().getName().equals(param.getSchoolClass())){
                    le[0]++;
                }
            }else{
                le[0]++;
            }
        }).collect(Collectors.toList());

        return le[0];
    }

    @Override
    public int passScoreByCollege(String sort,ProposalQueryParam param) {
        final int[] le = {0};
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<Scores> notifications = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(),sort1);
        List<ScoresDto> list= notifications.stream().map(this.scoresMapper::toDTO).collect(Collectors.toList()).stream().peek(n->{
            UserDto userDto=n.getStudentId().getUser();
            //设置用户的密码 防止密码返回真实的数据泄露
            userDto.setPassword(PASS_PLACE_HOLDER);
            StudentDto studentDto=n.getStudentId();
            studentDto.setUser(userDto);
            n.setStudentId(studentDto);

            if(param.getCollege()!=null&&!param.getCollege().equals("")&&!"null".equals(param.getCollege())){
                if(userDto.getSchoolClass().getCollege().getName().equals(param.getCollege())){
                    le[0]++;
                }
            }else{
                le[0]++;
            }
        }).collect(Collectors.toList());
        return le[0];
    }

    @Override
    public int passScoreByMajor(String sort,ProposalQueryParam param) {
        final int[] le = {0};
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<Scores> notifications = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(),sort1);
        List<ScoresDto> list= notifications.stream().map(this.scoresMapper::toDTO).collect(Collectors.toList()).stream().peek(n->{
            UserDto userDto=n.getStudentId().getUser();
            //设置用户的密码 防止密码返回真实的数据泄露
            userDto.setPassword(PASS_PLACE_HOLDER);
            StudentDto studentDto=n.getStudentId();
            studentDto.setUser(userDto);
            n.setStudentId(studentDto);
            if(param.getMajor()!=null&&!param.getMajor().equals("")&&!"null".equals(param.getMajor())){
                if(studentDto.getMajor().getName().equals(param.getCollege())){
                    le[0]++;
                }
            }else{
                le[0]++;
            }
        }).collect(Collectors.toList());
        return le[0];
    }

    @Override
    public int passScoreByScoresType(String sort,ProposalQueryParam param) {
        List<Sort.Order> orders = PageHelper.toSortOrderList(sort);
        Sort sort1 = Sort.by(orders);
        List<Scores> notifications = this.scoresRepository.findAll(
                BaseUtil.<Scores>getBuilder(param).build(),sort1);
        List<ScoresDto> list= notifications.stream().map(this.scoresMapper::toDTO).collect(Collectors.toList()).stream().peek(n->{
            UserDto userDto=n.getStudentId().getUser();
            //设置用户的密码 防止密码返回真实的数据泄露
            userDto.setPassword(PASS_PLACE_HOLDER);
            StudentDto studentDto=n.getStudentId();
            studentDto.setUser(userDto);
            n.setStudentId(studentDto);
        }).collect(Collectors.toList());
        return list.size();
    }
}
